﻿using System;
using NguyenHoa.Domains;
using NguyenHoa.Services;
using NguyenHoa.Web.App_Code;

namespace NguyenHoa.Web.Pages
{
  public partial class PromotionsDetailPage : BasePage
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack && !IsCallback)
      {
        LoadContent();
      }
    }

    #region Methods
    private void IncreaseViewCount(News news)
    {
      if (news == null)
      {
        return;
      }

      try
      {
        news.ViewCount = (news.ViewCount.HasValue) ? news.ViewCount.Value + 1 : 1;
        new NewsService().Update(news);
      }
      catch (Exception ex)
      {
        WriteLog("PromotionsDetailPage::Increase View Count", ex);
      }
    }

    private void LoadContent()
    {
      NewsService nService = new NewsService();
      Domains.News news = nService.GetById(Id);
      int? newsId = null;
      if (news != null)
      {
        newsId = news.Id;
        lblTitle.Text = news.Title;
        lblPageView.Text += "(";
        lblPageView.Text += (news.PostDate.HasValue) ? Utils.FormatDate(news.PostDate.Value) + " - " : string.Empty;
        lblPageView.Text += Resources.Strings.PageView + ": " + (news.ViewCount.HasValue ? news.ViewCount.Value : 0);
        lblPageView.Text += ")";
        lblSummary.Text = news.Summary;
        //if (!string.IsNullOrEmpty(news.ImagePath) && File.Exists(MapPath("~\\" + news.ImagePath)))
        //{
        //  divImage.Style["display"] = "block";
        //  image.ImageUrl = "~\\" + news.ImagePath;
        //}

        ltrNews.Text += news.Content;
        IncreaseViewCount(news);
      }

      #region Load Other News
      TList<Domains.News> otherNewsList = nService.GetOtherNews(null, CurrentLanguage.Id, newsId, GlobalCache.Instance.OtherNewsCount, "P");
      foreach (Domains.News otherNews in otherNewsList)
      {
        string url = "PromotionsDetailPage.aspx?id=" + otherNews.Id;
        string text = Utils.ReduceString(otherNews.Title, GlobalCache.Instance.ReduceStringLength);
        ltrOtherNews.Text += "<li><a href=\"" + url + "\">" + text + "</a>";
        text = "   (" + Utils.FormatDate(otherNews.PostDate.Value) + ")";
        ltrOtherNews.Text += "&nbsp;&nbsp<span class=\"news_detail_date\" style=\"font-style: italic; \">" + text + "</span></li>";
      }
      #endregion
    }
    #endregion
  }
}
